<template>
<div class="info">
  <el-form  :model='searchInfo' inline>
    <el-form-item>
      <el-input clearable v-model="searchInfo.actorName" placeholder="请输入演员姓名名" />
    </el-form-item>
    <el-form-item>
      <el-select v-model="searchInfo.status" placeholder="请选择">
        <el-option label="可用" value="1"></el-option>
        <el-option label="禁用" value="0"></el-option>
      </el-select>
    </el-form-item>
    <el-form-item>
      <el-button type="primary" icon="el-icon-search" @click="getList">查询</el-button>
      <el-button icon="el-icon-refresh-right" @click="resetForm">重置</el-button>
    </el-form-item>
    <el-form-item>
      <el-button type="success" @click="openWin()">添加</el-button>
    </el-form-item>
  </el-form>
  <el-table :data=actorList style="width: 100%" border >
    <el-table-column label="编号" prop="actorId"  align="center" width="50px" />
    <el-table-column label="演员名" prop="actorName"  align="center" />
    <el-table-column label="性别" prop="sex"  align="center" >
      <template slot-scope="scope">
        <el-button type="success" v-if="scope.row.sex===1">男</el-button>
        <el-button type="success" v-if="scope.row.sex===0">女</el-button>
        <el-button type="success" v-if="scope.row.sex===2">未知</el-button>
      </template>
    </el-table-column>
    <el-table-column label="生日" prop="birthday"  align="center" />
    <el-table-column label="介绍" prop="profile" align="center"/>
    <el-table-column label="状态" prop="status" align="center">
      <template slot-scope="scope">
        <el-button type="success" v-if="scope.row.status===1" @click="changStatus(scope.row)">启用</el-button>
        <el-button v-else type="danger" @click="changStatus(scope.row)" >禁用</el-button>
      </template>
    </el-table-column>
    <el-table-column label="操作">
      <template slot-scope="scope">
        <el-button type="success" @click="openWin(scope.row)">修改</el-button>
        <el-button type="danger" @click="delInfo(scope.row.actorId)">删除</el-button>
        <el-button type="primary" @click="searchActor(scope.row.actorId)">查看</el-button>
      </template>
    </el-table-column>
  </el-table>
  <el-pagination layout="total,prev,pager,next,sizes,jumper"
                 :total="pageInfo.totalCount"
                 :page-size="pageInfo.pageSize"
                 :current-page="pageInfo.currPage"
                 :page-sizes="[5,10,20,30,50,100]"
                 @size-change="sizeChange"
                 @current-change="currentChange"
  />
</div>
</template>

<script>
export default {
  name: 'Actor',
  data () {
    return {
      searchInfo: {
        status: '',
        actorName: '',
        page: 1,
        limit: 5
      },
      actorList: [],
      pageInfo: {}
    }
  },
  methods: {
    searchActor (id) {
      this.$router.push('ActorSearch?actorId=' + id)
    },
    getList () {
      this.$http.get('/sys/actor/list', this.searchInfo).then(({ data }) => {
        if (data.code === 200) {
          this.actorList = data.page.list
          this.pageInfo = data.page
        } else {
          this.$confirm(data.msg)
        }
      })
    },
    resetForm () {
      this.searchInfo.actorName = ''
      this.searchInfo.status = ''
      this.getList()
    },
    openWin (actorId) {
      if (actorId) {
        this.$router.push('ActorEdit?actorId=' + actorId.actorId)
      } else {
        this.$router.push('ActorEdit')
      }
    },
    changStatus (actor) {
      this.$confirm(`确定${actor.status === 1 ? '禁用' : '启用'}该演员吗？`).then(() => {
        this.$http.post('/sys/actor/status', { actorId: actor.actorId, status: actor.status === 1 ? 0 : 1 }).then(({ data }) => {
          if (data.code === 200) {
            this.$message.success('更改状态成功')
            this.getList()
          } else {
            this.$confirm(data.msg)
          }
        })
      })
    },
    sizeChange (limit) {
      this.searchInfo.limit = limit
      this.getList()
    },
    currentChange (page) {
      this.searchInfo.page = page
      this.getList()
    },
    delInfo (id) {
      this.$confirm('确定删除该演员吗', '询问').then(() => {
        this.$axios.delete(`/sys/actor/delete/${id}`).then(({ data }) => {
          if (data.code === 200) {
            this.$message({
              message: '删除成功',
              type: 'success',
              onClose: () => {
                this.getList()
              }
            })
          } else {
            this.$alert('删除失败!', { type: 'error' })
          }
        })
      })
    }
  },
  created () {
    this.$nextTick(() => {
      this.getList()
    })
  }
}
</script>

<style scoped>

</style>
